//
//  NSString+BPCrash.m
//  BPCrashDemo
//
//  Created by SiYugui on 2019/12/19.
//  Copyright © 2019年 SiYugui. All rights reserved.
//

#import "NSString+BPCrash.h"
#import "NSObject+BPSwizzle.h"
#import "BPCrashLog.h"

@implementation NSString (BPCrash)
+(void)avoidStringCrashExchangeMethod{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class __NSCFConstantString = NSClassFromString(@"__NSCFConstantString");
        
        [self exchangeInstanceMethod:__NSCFConstantString method1:@selector(characterAtIndex:) method2:@selector(__characterAtIndex:)];
        
        [self exchangeInstanceMethod:__NSCFConstantString method1:@selector(substringFromIndex:) method2:@selector(__substringFromIndex:)];
        
        [self exchangeInstanceMethod:__NSCFConstantString method1:@selector(substringToIndex:) method2:@selector(__substringToIndex:)];
        
        [self exchangeInstanceMethod:__NSCFConstantString method1:@selector(substringWithRange:) method2:@selector(__substringWithRange:)];
        
        [self exchangeInstanceMethod:__NSCFConstantString method1:@selector(stringByReplacingOccurrencesOfString:withString:options:range:) method2:@selector(__stringByReplacingOccurrencesOfString:withString:options:range:)];
        
        [self exchangeInstanceMethod:__NSCFConstantString method1:@selector(stringByReplacingCharactersInRange:withString:) method2:@selector(__stringByReplacingCharactersInRange:withString:)];
        
        [self exchangeInstanceMethod:__NSCFConstantString method1:@selector(stringByReplacingOccurrencesOfString:withString:) method2:@selector(__stringByReplacingOccurrencesOfString:withString:)];
    });
}

#pragma mark - characterAtIndex:
-(unichar)__characterAtIndex:(NSUInteger)index{
    unichar characteristic;
    @try {
        characteristic = [self __characterAtIndex:index];
    }
    @catch (NSException *exception) {
        
        [BPCrashLog noteErrorWithException:exception attachedToDo:BPCrashStringReturnUnichar];
    }
    @finally {
        return characteristic;
    }
}

#pragma mark - substringToIndex:
-(NSString *)__substringToIndex:(NSUInteger)to{
    NSString *subString = nil;
    
    @try {
        subString = [self __substringToIndex:to];
    }
    @catch (NSException *exception) {
        [BPCrashLog noteErrorWithException:exception attachedToDo:BPCrashDefaultReturnNil];
        subString = nil;
    }
    @finally {
        return subString;
    }
}

#pragma mark - substringFromIndex:
-(NSString *)__substringFromIndex:(NSUInteger)from{
    NSString *subString = nil;
    
    @try {
        subString = [self __substringFromIndex:from];
    }
    @catch (NSException *exception) {
        [BPCrashLog noteErrorWithException:exception attachedToDo:BPCrashDefaultReturnNil];
        subString = nil;
    }
    @finally {
        return subString;
    }
}

#pragma mark - substringWithRange:
-(NSString *)__substringWithRange:(NSRange)range{
    NSString *subString = nil;
    
    @try {
        subString = [self __substringWithRange:range];
    }
    @catch (NSException *exception) {
        NSString *defaultToDo = BPCrashDefaultReturnNil;
        [BPCrashLog noteErrorWithException:exception attachedToDo:defaultToDo];
        subString = nil;
    }
    @finally {
        return subString;
    }
}

#pragma mark - stringByReplacingCharactersInRange:withString:

-(NSString *)__stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement{
    NSString *newment = nil;
    
    @try {
        newment = [self __stringByReplacingCharactersInRange:range withString:replacement];
    }
    @catch (NSException *exception) {
        [BPCrashLog noteErrorWithException:exception attachedToDo:BPCrashDefaultReturnNil];
        newment = nil;
    }
    @finally {
        return newment;
    }
}

#pragma mark - stringByReplacingOccurrencesOfString: withString: options:range:

-(NSString *)__stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange{
    NSString *newment = nil;
    
    @try {
        newment = [self __stringByReplacingOccurrencesOfString:target withString:replacement options:options range:searchRange];
    }
    @catch (NSException *exception) {
        [BPCrashLog noteErrorWithException:exception attachedToDo:BPCrashDefaultReturnNil];
        newment = nil;
    }
    @finally {
        return newment;
    }

}

#pragma mark - stringByReplacingOccurrencesOfString: withString:

-(NSString *)__stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement{
    NSString *newment = nil;
    
    @try {
        newment = [self __stringByReplacingOccurrencesOfString:target withString:replacement];
    }
    @catch (NSException *exception) {
        [BPCrashLog noteErrorWithException:exception attachedToDo:BPCrashDefaultReturnNil];
        newment = nil;
    }
    @finally {
        return newment;
    }
}

@end
